package com.practice.线程的几种状态示例;
/**
 * @program: tenghw-practice-demo
 * @author: tenghw
 * @create: 2024-08-07 18:10
 * @Version 1.0
 * @description: 线程的几种状态示例 - Blocked阻塞状态
 **/
public class BlockedDemo {
        private static final Object lock = new Object();
        public static void main(String[] args) {

            Thread threadA = new Thread(() -> {
                System.out.println("Thread A: Trying to acquire lock");
                synchronized (lock) {
                    System.out.println("Thread A: Acquired lock");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread A: Releasing lock");
                }
            });

            Thread threadB = new Thread(() -> {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread B: Trying to acquire lock");
                synchronized (lock) {
                    System.out.println("Thread B: Acquired lock");
                    System.out.println("Thread B: Releasing lock");
                }
            });

            threadA.start();
            threadB.start();
        }
    }

